package com.guli.gulimall.cart.controller;

import com.guli.gulimall.cart.service.CartService;
import com.guli.gulimall.cart.vo.Cart;
import com.guli.gulimall.cart.vo.CartItem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.concurrent.ExecutionException;

@Controller
public class CartController {

    @Autowired
    private CartService cartService;

    @GetMapping("/currentUserCartItems")
    @ResponseBody
    public List<CartItem> currentUserCartItems(){
        return cartService.getUserCartItems();
    }

    /**
     * 去购物车页面
     * <p>
     * 浏览器有一个cookie:user_key:标识用户身份，一个月后过期；
     * 如果第一次使用京东的购物车功能，都会给用户一个身份；
     * 浏览器以后保存，每次访问都会带上这个cookie
     * <p>
     * 登录：从session中查到用户
     * <p>
     * 未登录：按照cookie里面带来的user-key来做。
     * 第一次：没有临时用户，需要创建一个user-key。
     *
     * @param model
     * @return
     */
    @GetMapping({"/", "/cart.html"})
    public String cartListPage(Model model, HttpSession session) throws ExecutionException, InterruptedException {
        //快速得到用户信息
//        UserInfoTo userInfoTo = CartInterceptor.threadLocal.get();
//        System.out.println(userInfoTo.toString());

        Cart cart = cartService.getCart();
        model.addAttribute("cart", cart);
        return "cartList";
    }

    /**
     * 添加商品到购物车
     * <p>
     * RedirectAttributes ra
     * ra.addFlashAttribute //将数据放在session中，页面中可以取出,但是只能取一次。
     * ra.addAttribute     //将数据放在URL后面
     *
     * @param skuId 商品id
     * @param num   商品数量
     * @return
     */
    @GetMapping("/addCartItem")
    public String addToCart(@RequestParam("skuId") Long skuId, @RequestParam("num") Integer num, Model model, RedirectAttributes redirectAttributes) throws ExecutionException, InterruptedException {
        CartItem cartItem = cartService.addToCart(skuId, num);
        redirectAttributes.addAttribute("skuId", skuId);

        return "redirect:http://cart.gulimall.com/addCartSuccess.html";
    }

    /**
     * 购物车商品勾选
     * @param isChecked
     * @param skuId
     * @return
     */
    @RequestMapping("/checkCart")
    public String checkCart(@RequestParam("isChecked") Integer isChecked,@RequestParam("skuId")Long skuId) {
        cartService.checkCart(skuId, isChecked);
        return "redirect:http://cart.gulimall.com/cart.html";
    }

    @GetMapping("/countItem")
    public String countItem(@RequestParam("skuId")Long skuId,@RequestParam("num")Integer num){
        cartService.changeItemCount(skuId,num);

        return "redirect:http://cart.gulimall.com/cart.html";
    }

    /**
     * 删除购物车商品
     * @return
     */
    @GetMapping("/deleteItem")
    public String deleteItem(@RequestParam("skuId")Long skuId){
        cartService.deleteItem(skuId);
        return "redirect:http://cart.gulimall.com/cart.html";
    }


    /**
     * 跳转到成功页面,做这个页面的目的就是防止重复提交
     *
     * @param skuId
     * @param model
     * @return
     */
    @GetMapping("/addCartSuccess.html")
    public String addToCartSuccessPage(@RequestParam("skuId") Long skuId, Model model) {
        //重定向到成功页面，再次查询购物车数据
        CartItem cartItem = cartService.getCartItem(skuId);
        model.addAttribute("item", cartItem);

        return "success";
    }

}
